﻿using System.Windows.Controls;

namespace Might.UIControls.ObjectBrowser {
    public partial class ObjectBrowserView : IObjectBrowserView {
        public ObjectBrowserView() {
            InitializeComponent();

            ObjectListBox.SelectionChanged += ObjectListBoxSelectionChanged;
        }

        #region IObjectBrowserView Members

        public event ObjectBrowserSelectedEventHandler ObjectSelected;

        public void AddItem(ListBoxItem item) {
            ObjectListBox.Items.Add(item);
        }

        public void Clear() {
            ObjectListBox.Items.Clear();
        }

        #endregion

        private void ObjectListBoxSelectionChanged(object sender, SelectionChangedEventArgs e) {
            ListBoxItem item = ObjectListBox.SelectedItem as ListBoxItem;
            if (item != null) {
                OnObjectSelected(item.Tag);
            }
        }

        protected virtual void OnObjectSelected(object selectedData) {
            if (ObjectSelected != null) {
                ObjectSelected(selectedData);
            }
        }
    }
}
